const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab:0,
        dotation:false,
        use:false,
      
        navData:[
     
            {
                text: '未支付',
                status:0
            },
            {
                text: '已支付',
                status:1
            },
            {
                text: '已取消',
                status:2
            },
            {
                text: '已退款',
                status:3
            },
           
           
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        var status = options.status;
        var navData = this.data.navData;
        console.log(status);
        if(status == undefined){
            status=0;
        }
        if(status == ''){
          status=0;
        }
    
        this.setData({
            currentTab:status,
            status:status
        })
      this.getList(status)
        var open=wx.getStorageSync('open');
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })             
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },
    switchNav(event){
        var cur = event.currentTarget.dataset.current;
        console.log(cur);  
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.width / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })      
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
            this.getList(cur);
          
        }
    },
    switchTab(event){
        var cur = event.detail.current;
        var singleNavWidth = this.data.width / 5;
        var left = (cur - 2) * singleNavWidth;
        this.setData({
            currentTab: cur,
            navScrollLeft: left
        });
    },
    goBack:function(){
        wx.navigateBack();
      },
  
      showModals:function(msg,url,index){
        wx.showModal({
            title: '系统提示',
            content:msg,
            success:function(res){
              console.log(res)
               if(res.confirm){
                if(url){
                  if(index == 'index'){
                    wx.reLaunch({
                      url: url,
                    })
                  }else{
                    wx.navigateTo({
                      url: url,
                    })
                  }
                }
               }
               if(res.cancel){
    
               }
            }
        })
    },
    updateOrder(e){
      var id = e.currentTarget.dataset.id;
      var msg = e.currentTarget.dataset.msg;
      var status = e.currentTarget.dataset.status;
      var open = wx.getStorageSync('open');
      let that = this;
      wx.showModal({
        title: '系统提示',
        content: msg,
        complete: (res) => {
          if (res.cancel) {
            
          }
      
          if (res.confirm) {
            wx.request({
              url: app.globalData.domain+'wechat/order/updateOrder',
              data:{from:1,open_id:open.id,id:id,status:status},
              success:res=>{
                var data=res.data;
                if(data.code==0){
                  that.showModals(data.msg,data.url,data.index);
                 // that.getList();
                }else{
                  that.showModals(data.msg,data.url,data.index);
                }
                //console.log(data);
              }
            })
          }
        }
      })
      
    },
    gotoPay(e){
      var id = e.currentTarget.dataset.id;
      var msg = e.currentTarget.dataset.msg;
      var status = e.currentTarget.dataset.status;
      var open = wx.getStorageSync('open');
      let that = this;
      wx.showModal({
        title: '系统提示',
        content: msg,
        complete: (res) => {
          if (res.cancel) { }
      
          if (res.confirm) {
             wx.request({
              url: app.globalData.domain+'wechat/pay/payOrder',
              data:{from:1,open_id:open.id,openid:open.openid,id:id,status:status},
              success:res=>{
                var data=res.data;
                if(data.code==200){
                  var data=res.data.data;
                  wx.requestPayment({
                      'timeStamp': data.timeStamp,
                      'nonceStr': data.nonceStr,
                      'package': data.package,
                      'signType': 'MD5',
                      'paySign': data.paySign,
                      'success': function (res) {
                       // console.log('success');
                        wx.showToast({
                          title: '支付成功',
                          icon: 'success',
                          duration: 3000
                        });
                       // wx.setStorageSync('shopcart','')
                        setTimeout(function() {
                            //要延时执行的代码
                            wx.navigateTo({
                                url: 'mylist?status=1',
                            })
                          }, 1000) //延迟时间
                        //that.waitPayShow();
                      },
                      'fail': function (res) {
                        //console.log(res);
                      },
                      'complete': function (res) {
                       // console.log('complete');
                      }
                    });
                 // that.getList();
                }else{
                  that.showModals('订单状态错误','#','#');
                }
                //console.log(data);
              }
            })
          }
        }
      })
    },
    refund(e){
        console.log(e);
        var id=e.currentTarget.dataset.id
        var coupon_no=e.currentTarget.dataset.coupon_no;
        var open=wx.getStorageSync('open');
        var page=this.data.page;
        let that=this;
        wx.request({
           url: app.globalData.domain+'pay/refund',
           data:{from:1,user_id:open.id,id:id,coupon_no:coupon_no},
           success:res=>{    
              // console.log(res);
              var data=res.data;
              if(data.code=200){
                   that.showModals(data.desc)
                   that.getList()
              }else{
                   that.showModals(data.desc)
                   that.getList()
              }
           }
       });

    },
    goto:function(e){
      console.log(e);
      var url=e.currentTarget.dataset.url;
      var parent_id=wx.getStorageSync('parent_id');
      var index=e.currentTarget.dataset.index;
      if(url=="#"){

      }else if(url=='phone'){
          wx.makePhoneCall({
            phoneNumber: this.data.store.phone,
          })
      }else if(url=='coupon'){
          var date=util.formatTime(new Date());
          var face_value=e.currentTarget.dataset.face_value;
          var pay_money=e.currentTarget.dataset.pay_money;
          var yxq=e.currentTarget.dataset.yxq;
          var month=e.currentTarget.dataset.month;
           log.info("Time:"+date+",page:index,function:goto:,index"+index);
          this.getCoupon(face_value,month,yxq,pay_money);
      }else{
          if(index=='index'){
              wx.reLaunch({
                  url: url,
                })
          }else{
              console.log(url);
              wx.navigateTo({
                  url: url,
                })
          }
     
      }
    // console.log(url);
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       
    },
  getList(status){
      let that=this;
      let data=this.data;
      var open = wx.getStorageSync('open');
     
      wx.request({
        url: app.globalData.domain+'wechat/order/getMyList',
        data:{from:1,open_id:open.id,status:status},
        success:res=>{
          var data=res.data;
          console.log(data);
          that.setData({
            list:data.list,
            count:data.count
          })
        }
    })
  },
  goto:function(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var parent_id=wx.getStorageSync('parent_id');
    if(url=="#"){

    }else if(url=='use'){
        var coupon_no=e.currentTarget.dataset.coupon_no;
        var id=e.currentTarget.dataset.id;
        wx.navigateTo({
          url: 'use?coupon_no='+coupon_no+'&id='+id,
        })
    }else if(url=='phone'){
        wx.makePhoneCall({
          phoneNumber: '400-800-2177',
        })
    }else{
        wx.navigateTo({
          url: url,
        })
    }
    console.log(url);
},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})